﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.GetAction("ETL", "GetDataServiceList", "1.0")]
    public class GetDataServiceListAction : BaseAction<GetDataServiceListParams, GetDataServiceListResult>
    {
        protected override ActionResult<GetDataServiceListResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataServiceListParams opParams)
        {
            GetDataServiceListResult result = new GetDataServiceListResult();

            SER.DtSerService ser = new SER.DtSerService();

            PagedList<VIW.VDtSer> serResult = ser.GetDtSerList(opParams.FolderId, opParams.Filter, opParams.Page * 25, 25);

            if(serResult.IsSuccess)
            {
                result.Start = serResult.Start;
                result.Limit = serResult.Limit;
                result.Total = serResult.Total;
                result.Count = serResult.Data.Count;

                foreach (var model in serResult.Data)
                {
                    GetDataServiceListResult.Item item = new GetDataServiceListResult.Item();
                    item.Id = model.Id;
                    item.FolderId = model.FolderId;
                    item.IsDeleted = model.IsDeleted;
                    item.IsEnabled = model.IsEnabled;
                    item.Name = model.Name;
                    item.TypeId = model.TypeId;
                    item.NeedAuth = model.NeedAuth;
                    item.CategoryId = model.CategoryId;
                    item.Address = model.Address;
                    item.MappedCount = model.MappedCount;
                    item.UnmappedCount = model.UnmappedCount;
                    result.Items.Add(item);
                }
                return new ActionResult<GetDataServiceListResult>(result);
            }
            else
            {
                return new ActionResult<GetDataServiceListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }

        private GetDataServiceFolderTreeResult.Item Convert(MOD.MDtSerFolder folder)
        {
            GetDataServiceFolderTreeResult.Item item = new GetDataServiceFolderTreeResult.Item();
            item.Id = folder.Id;
            item.Caption = folder.Caption;
            item.Code = folder.Code;

            foreach (var child in folder.Children)
            {
                item.Items.Add(Convert(child));
            }
            return item;
        }
    }

    public class GetDataServiceListParams
    {
        [Parameter]
        public Guid FolderId { get; set; }

        public string Filter { get; set; }

        [Parameter]
        [Default(0)]
        public int Page { get; set; }
    }

    public class GetDataServiceListResult : PagedResult
    {
        public List<Item> Items { get; set; }

        public GetDataServiceListResult()
        {
            Items = new List<Item>();
        }

        public class Item
        {
            public Guid Id { get; set; }

            public Guid FolderId { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }

            public string Address { get; set; }

            public bool NeedAuth { get; set; }

            public string AuthUser { get; set; }

            public string AuthPassword { get; set; }

            public bool IsDeleted { get; set; }

            public bool IsEnabled { get; set; }

            public string Creator { get; set; }

            public DateTime CreatedTime { get; set; }

            public Guid CategoryId { get; set; }

            public Guid TypeId { get; set; }

            public int MappedCount { get; set; }

            public int UnmappedCount { get; set; }
        }
    }
}
